using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace CommonSLT
{
	public delegate void dgMyProgressShowProgress(int total);
	public delegate void dgMyProgressSetValue();
	public delegate void dgMyProgressSetStop();

	public partial class MyProgress : DevExpress.XtraEditors.XtraUserControl
	{
		public dgMyProgressShowProgress myProgressShowProgress = null;
		public dgMyProgressSetValue myProgressSetValue = null;
		public dgMyProgressSetStop myProgressSetStop = null;
		public OnProgressCompletedEventHandler OnProgressCompleted = null;

		private XtraForm form = null;
		private bool isFirst = false;
		private int currValue = 0;
		private int total = 0;

		public MyProgress(XtraForm form)
		{
			InitializeComponent();

			this.myProgressShowProgress = new dgMyProgressShowProgress(ShowProgress);
			this.myProgressSetValue = new dgMyProgressSetValue(SetValue);
			this.myProgressSetStop = new dgMyProgressSetStop(SetStop);

			this.form = form;
			this.Visible = false;
			this.Load += new EventHandler(MyProgress_Load);
			this.Left = form.Width / 2 - this.Width / 2;
			this.Top = form.Height / 2 - this.Height / 2;
		}

		private void MyProgress_Load(object sender, EventArgs e)
		{
			this.BringToFront();
		}

		public void ShowProgress(int total)
		{
			this.currValue = 0;
			this.total = total;
			this.isFirst = false;
			this.pncenter.Width = 0;
			this.pnLeft.BackgroundImage = global::CommonSLT.Properties.Resources.rc_main_btn_clean_left;
			this.pnRight.BackgroundImage = global::CommonSLT.Properties.Resources.rc_main_btn_clean_right;
			Utility.SetDisbaleForm(this.form, true);
			this.Visible = true;
		}

		public void SetValue()
		{
			this.currValue++;
			double percent = this.currValue / this.total;
			this.pncenter.Width = (int)Math.Floor(this.pnCenterMain.Width * percent);

			if (this.isFirst == false)
				if (this.pncenter.Width > 0)
				{
					this.isFirst = true;
					this.pnLeft.BackgroundImage = global::CommonSLT.Properties.Resources.rc_main_btn_clean_left_green;
					this.pnRight.BackgroundImage = global::CommonSLT.Properties.Resources.rc_main_btn_clean_right_green;
				}

			if (this.currValue == this.total)
			{
				this.Visible = false;
				Utility.SetDisbaleForm(this.form, false);

				if (this.OnProgressCompleted != null)
					this.OnProgressCompleted(this, new EventArgs());
			}
		}

		public void SetStop()
		{
			if (this.currValue == this.total)
			{
				this.Visible = false;
				Utility.SetDisbaleForm(this.form, false);

				if (this.OnProgressCompleted != null)
					this.OnProgressCompleted(this, new EventArgs());
			}
		}
	}
}
